package com.leyou.item.controller;

import com.leyou.Item;
import com.leyou.common.enums.ExceptionEnum;
import com.leyou.common.exceptions.LyException;
import com.leyou.item.service.ItemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @package com.leyou.item.controller
 * @description:
 * @author: 86157
 * @date 2019/6/22 21:11
 * @version: V1.0
 */
@RestController
public class ItemController {

    @Autowired
    private ItemService itemService;

    @PostMapping("item")
    public ResponseEntity<Item> saveItem(Item item) {
        //如果价格为空，则抛出异常，返回400状态，请求参数有误
        if (item.getPrice() == null) {
            // return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(null);
           //   throw new RuntimeException("价格不能为空");
            throw new LyException(ExceptionEnum.INVALID_ORDER_STATUS);
        }
        Item result = itemService.saveItem(item);
        return ResponseEntity.status(HttpStatus.CREATED).body(result);
    }
}